//
//  HWBookViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/1/8.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWBookViewController.h"
#import "HWBookView.h"
#import "HWLoginViewController.h"

@interface HWBookViewController ()
@property (nonatomic, strong) HWBookView *hwBookView;
@end

@implementation HWBookViewController

- (HWBookView *)hwBookView
{
    if (!_hwBookView)
    {
        _hwBookView = [[HWBookView alloc] initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-TABBAR_HEIGHT)];
        WeakSelf(self)
        _hwBookView.upLoadIndexBlcok = ^(NSString *page, NSString *size) {
            [weakSelf getInfor:page page:size];
        };
        _hwBookView.refreshBookShelfBlock = ^{
            [weakSelf getInfor:@"1" page:@"10"];
        };
    }
    return _hwBookView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"嗨书架"];

    [self.view addSubview:self.hwBookView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getInfor:@"1" page:@"10"];
    [self buryPoint];
}

#pragma mark - 获取嗨书架信息
- (void)getInfor:(NSString *)pageNum page:(NSString *)pagesize
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"pageNum" :pageNum,
                               @"pageSize" : pagesize
                               };
    
    [SVProgressHUD showWithStatus:@"加载中"];
    WeakSelf(self)
    [MyBookShelfManager getBookShelfInfo:prarmers sucess:^(id  _Nonnull acceptData) {
        NSNumber *nub = IFNULL(acceptData[@"hasNextPage"], [NSNumber new]);
        NSArray *arr = [NSArray arrayWithArray:acceptData[@"list"]];
        [weakSelf.hwBookView getDate:arr hasNetPage:nub];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"获取失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
  [HWUtilties buryPointWithStayTime:nowStayTime
                          timestamp:[HWUtilties getNowTimeTimestamp3]
                           memberId:current_User.userID
                            fromUrl:self.fromUrl
                              toUrl:@"/bookShelf"
                             fromId:self.fromId
                               toId:@"db8c1b12eff98775"];
}

@end
